<?php

	include_once("$config[webroot]/includes/page_utf_class.php");
	include_once($config['webroot']."/module/activity/includes/plugin_activity_class.php");

	if($_GET['operation']=="add" or $_GET['operation']=="edit")
	{
		if($_POST['act'])
		{	
		    $activity=new activity();
		    if($activity->get_activity_spike_buy_skuid($_POST['sku_id'],10,$_POST['id'])){
		        msg("?m=activity&s=promote.php&operation=".$_GET['operation'].($_GET['operation']=="edit"?"&editid=".$_GET['editid']:""),"此商品已经在秒杀了");
		    }
			unset($_GET['s']);
			unset($_GET['m']);
			unset($_GET['operation']);
			if($_POST['stime'] == $_POST['etime'])
			{
			    //@todo:促销是否可以跨天的判断
			    
			}
			$_POST['stime'].=(' '.$_POST['startHour'].':00:00');
			$_POST['etime'].=(' '.$_POST['endHour'].':59:59');
			
			if($_POST["act"]=='save')
			{
					$activityspike = $activity->add_activity_spike(10);
				}

			if($_POST["act"]=='edit' and is_numeric($_POST['id']))
			{
				$activityspike =$activity->edit_activity_spike();
				unset($_GET['editid']);
			}
			$getstr=implode('&',convert($_GET));
			msg("?m=activity&s=promote.php&$getstr");
		}
		if($_GET['editid'] and is_numeric($_GET['editid']))
		{      
			$sql="select a.*,b.pname,b.price,b.market_price,b.stock from ".ACTIVITYSPIKE."  as a left join ".SETMEAL." as b on a.sku_id=b.id  where a.id='$_GET[editid]'";
			$db->query($sql);
			$de=$db->fetchRow();
		}
    	
	}
	else
	{
		if($_GET['delid'])
		{
		    $activity=new activity();
		    $activity->del_activity_spike($_GET['delid']);
			unset($_GET['delid']);
			$getstr=implode('&',convert($_GET));
			msg("?m=activity&s=promote.php&$getstr");
		}
		if($_POST['act']=='op')
		{
			if(is_array($_POST['chk']))
			{
				foreach($_POST['chk'] as $list)
				{
					$activity=new activity();
		            $activity->del_activity_spike($list);
				}
			}
			
			msg("?m=activity&s=promote.php");
		}	
		
		
		//获取促销的条件------------------------------------
		$where = " ";
		
		if(!empty($_GET[product_id])){
		  $where.=" and a.pid in($_GET[product_id])";
		}
		if($_GET['status']!==NULL&&$_GET['status']!==''){
		    $where .=" and a.status='$_GET[status]'";
		}
		if($_GET['sdate']!='' && $_GET['edate']!='' && (strtotime($_GET['sdate'])<=strtotime($_GET['edate']))){
		    $where .=" and a.start_time>='".($_GET['sdate'])."' and a.end_time<='".(date("Y-m-d H:i:s",strtotime($_GET['edate'])+86399))."'";
		}else{
		    if($_GET['sdate']!='' && $_GET['edate']==''){
		        $where .=" and a.start_time>='".($_GET['sdate'])."'";
		    }
		    if($_GET['sdate']=='' && $_GET['edate']!=''){
		        $where .=" and a.end_time<='".(date("Y-m-d H:i:s",strtotime($_GET['edate'])+86399))."'";
		    }
		}
		if($_GET['is_start']!=''){
		   $now_time = date("Y-m-d H:i:s");
		    switch ($_GET['is_start']){
		        case -1:
		            $where .= " and a.end_time<'".$now_time."'";break;
		        case 1:
		            $where .= " and a.start_time<='".$now_time."' and a.end_time>='".$now_time."'";break;
		        case 2:
		            $where .= " and a.start_time>'".$now_time."'";break;
		        default:
		            break;
		    }
		}
		//------------------------------------
		
	$noPageSql = $sql="select a.*,b.pic,b.catid,b.pname,b.price,b.market_price,b.spec from ".ACTIVITYSPIKE."  as a left join ".SETMEAL." as b on a.sku_id=b.id   where a.status>-9 and a.type=10  {$where} order by ABS(a.start_time-NOW())";
		$page = new Page;
		$page->listRows=20;
		if (!$page->__get('totalRows'))
		{
			$db->query($sql);
			$page->totalRows = $db->num_rows();
		}
		$sql .= "  limit ".$page->firstRow.",".$page->listRows;
		$db->query($sql);
		$de['list']=$db->getRows();
		
		if($de['list'])
		{
			foreach($de['list'] as $key=>$val)
			{
	   			 $tmp=array();
	    		if ($val['catid']){
	        		$sql = "select cat from ".PCAT." where '$val[catid]' like concat(`catid`,'%')  order by catid asc";
	        		$db->query($sql);
	        		foreach ($cat=$db->getRows() as $item){
	           			 $tmp[]=$item['cat'];
	        		}
	    		}
	    		
	     
	    		$de['list'][$key]['cat']=implode('->', $tmp);
	    		if($val['pic']){
	    		    $vv=explode(',', $val['pic']);
	    		    $de['list'][$key]['pic']=$vv[0];
	    		    
	    		}
			}
		}
		$de['page']=$page->prompt();
		
		$db->query($noPageSql);
		$allPro = $db->getRows();
		$productIds = "";
		foreach ($allPro as $val)
		{
	    		$productIds .= empty($productIds)?$val['pid']:','.$val['pid'];
		}
		
    	$tpl->assign("productIds",$productIds);
	}
	$tpl->assign("de",$de);
	$tpl->assign("config",$config);
	$tpl->display("promote.htm");

?>